(function(plugIn) {

  jsx3.$O(plugIn).extend({
    
    getWizard: function() {
      return this._wizard;
    },
    
    launch: function() {

      var container = this.loadRsrcComponent("container", this.getServer().getRootBlock()/* layout */);
      
      jsx3.sleep(function() { this.loadWizard(container) }, null, this);
    },
    
    loadWizard: function(container) {

      var Wizard = com.tibco.amx.Wizard;
      var WizardPane = com.tibco.amx.WizardPane;
      
      var wizard = this._wizard = new Wizard();
      wizard.setRandomAccess(true);

      jsx3.$A(["pane1", "pane2", "pane3", "pane4"]).each(jsx3.$F(function(e) {
        var rsrc = this.getResource(e);
        var images = rsrc.attr("images");
        images = images ? images.split(/\s+/g) : null;
        var pane = new WizardPane(rsrc, this, {title:rsrc.attr("title"), images:images});
        wizard.addPane(pane);
      }).bind(this));

      wizard.renderIn(container.getWizardContainer());

      wizard.subscribe(Wizard.CANCEL, this, function() {
        container.doClose();
        this._clear();
        this.getServer().alert(null, "Canceled!");
      });

      wizard.subscribe(Wizard.FINISH, this, function() {
        container.doClose();
        var session = wizard.getSession();
        var s = ['<strong>Return Values:</strong><br/>'];
        for (var f in session)
          s.push("<strong>" + f + "</strong> : " + session[f]);
        this.getServer().alert("<strong>Finished!</strong>", "<br/>" + s.join('<br/>') + "<br/>", null, null, {width:600, height:400});
      });
    },
    
    _clear: function() {
      this._wizard = null;
    }
    
  });

})(this);
